#include "playersManager.h"

//------------------------------
playersManager::playersManager(gamePlay* _myGamePlay,soundManager* _mySoundManager, int _totalTeamPlayers)
{
	mySoundManager = _mySoundManager;
	totalTeamPlayers = _totalTeamPlayers;
	if(totalTeamPlayers<=0) totalTeamPlayers=1;
	if(totalTeamPlayers>4) totalTeamPlayers=4;

	myPlayersTeamA = new player[totalTeamPlayers];
	myPlayersTeamB = new player[totalTeamPlayers];

	myGamePlay = _myGamePlay;
	initialPosition();
}

//------------------------------
void playersManager::initialPosition()
{	
	for(int i=0;i<totalTeamPlayers;i++)
	{
		myPlayersTeamA[i].setPosX(3-i);
		myPlayersTeamA[i].setPosY(9);
		myPlayersTeamA[i].setTeam(1);
		myPlayersTeamA[i].setId(i);
		myPlayersTeamA[i].setLife(true);
	}
	for(int j=0;j<totalTeamPlayers;j++)
	{
		myPlayersTeamB[j].setPosX(6+j);
		myPlayersTeamB[j].setPosY(9);
		myPlayersTeamB[j].setTeam(2);
		myPlayersTeamB[j].setId(j);
		myPlayersTeamB[j].setLife(true);
		//sendDeadPlayer(int id,int team, int status)
	}
	
}

//------------------------------
playersManager::~playersManager()
{

}

//------------------------------
void playersManager::update()
{
	
	// players position
	for(int i=0;i<totalTeamPlayers;i++)
	{
		int posX = myPlayersTeamA[i].getPosX();
		int posY = myPlayersTeamA[i].getPosY();
		//look if a pixel kill player
		if( myPlayersTeamA[i].getLife() && myGamePlay->getIsKilled(posX,posY,1)  )
		{
			myPlayersTeamA[i].setLife( false );
			mySoundManager->dead(1);
		}
		if( myPlayersTeamA[i].getLife() ){
			int stateShooting = myPlayersTeamA[i].getShooting();
			if(stateShooting ==0)
			{
				myGamePlay->lightColorWindow( posX, posY, 0,255,0);
			}else{
				myGamePlay->lightWindow( posX, posY, (int)(255.0f*((float)stateShooting/20.0f)));
			}
		}
	}
	for(int i=0;i<totalTeamPlayers;i++)
	{
		int posX = myPlayersTeamB[i].getPosX();
		int posY = myPlayersTeamB[i].getPosY();
		if( myPlayersTeamB[i].getLife() && myGamePlay->getIsKilled(posX,posY,2) )
		{
			myPlayersTeamB[i].setLife( false );
			mySoundManager->dead(2);
		}
		if( myPlayersTeamB[i].getLife() ){
			int stateShooting = myPlayersTeamB[i].getShooting();
			if(stateShooting ==0)
			{
				myGamePlay->lightColorWindow( posX, posY, 255,0,0);
			}else{
				myGamePlay->lightWindow( posX, posY, (int)(255.0f*((float)stateShooting/20.0f)));
			}
		}
	}

	// check for gameOver
	myGamePlay->setTotalLifePlayersTeamA(getTotalLifePlayers(1));
	myGamePlay->setTotalLifePlayersTeamB(getTotalLifePlayers(2));

}



//------------------------------
int playersManager::getTotalLifePlayers(int team)
{
	int result = 0;
	for(int i=0;i<totalTeamPlayers;i++)
	{
		if(team == 1 && myPlayersTeamA[i].getLife())result+=1;
		if(team == 2 && myPlayersTeamB[i].getLife())result+=1;
	}
	return result;
}

//------------------------------
void playersManager::shootingPlayer(int team, int id, int value)
{
	if( myGamePlay->getIsGameStart() && myPlayersTeamA[id].getLife() )
	{
		mySoundManager->shooting(team,id);
		if(team==1)
		{
			myPlayersTeamA[id].setShooting(value);
		}
		if(team==2)
		{
			myPlayersTeamB[id].setShooting(value);
		}
	}
}

//------------------------------
void playersManager::shootFailPlayer(int team, int id)
{
	mySoundManager->shootingStop(team,id);
}

//------------------------------
void playersManager::shootedPlayer(int team, int id)
{
	if( myGamePlay->getIsGameStart() && myPlayersTeamA[id].getLife() )
	{
		mySoundManager->shootingStop(team,id);
		mySoundManager->shoot();
		if(team==1)
		{	
			myGamePlay->addBlock('a',myPlayersTeamA[id].getPosX(), myPlayersTeamA[id].getPosY());
		}
	}
	if( myGamePlay->getIsGameStart() && myPlayersTeamB[id].getLife() )
	{
		if(team==2)
		{
			myGamePlay->addBlock('b',myPlayersTeamB[id].getPosX(), myPlayersTeamB[id].getPosY());
		}
	}
}

//------------------------------
void playersManager::movePlayer(int action,int team, int id)
{
	// TEAM A
	if(team==1)
	{
		//UP
		if(
			action==1
			&& id<totalTeamPlayers 
			&& !checkPositionTeamA(myPlayersTeamA[id].getPosX(),myPlayersTeamA[id].getPosY()-1 )
			&& (myPlayersTeamA[id].getPosY()-1)>=0
		)
		{
			myPlayersTeamA[id].setPosY( myPlayersTeamA[id].getPosY()-1);
		}
		//DOWN
		if(
			action==2 
			&& id<totalTeamPlayers 
			&& !checkPositionTeamA(myPlayersTeamA[id].getPosX(),myPlayersTeamA[id].getPosY()+1 )
			&& (myPlayersTeamA[id].getPosY()+1)<=18
		)
		{
			myPlayersTeamA[id].setPosY( myPlayersTeamA[id].getPosY()+1);
		}
		//LEFT
		if(
			action==4 
			&& id<totalTeamPlayers 
			&& !checkPositionTeamA(myPlayersTeamA[id].getPosX()+1 ,myPlayersTeamA[id].getPosY())
			&& (myPlayersTeamA[id].getPosX()+1)<=3
		)
		{
			myPlayersTeamA[id].setPosX( myPlayersTeamA[id].getPosX()+1);
		}
		//RIGHT
		if(
			action==3
			&& id<totalTeamPlayers 
			&& !checkPositionTeamA(myPlayersTeamA[id].getPosX()-1 ,myPlayersTeamA[id].getPosY() )
			&& (myPlayersTeamA[id].getPosX()-1)>=0
		)
		{
			myPlayersTeamA[id].setPosX( myPlayersTeamA[id].getPosX()-1);
		}
	}
	// TEAM B
	if(team==2)
	{
		//UP
		if(
			action==1
			&& id<totalTeamPlayers 
			&& !checkPositionTeamB(myPlayersTeamB[id].getPosX(),myPlayersTeamB[id].getPosY()-1 )
			&& (myPlayersTeamB[id].getPosY()-1)>=0
		)
		{
			myPlayersTeamB[id].setPosY( myPlayersTeamB[id].getPosY()-1);
		}
		//DOWN
		if(
			action==2 
			&& id<totalTeamPlayers 
			&& !checkPositionTeamB(myPlayersTeamB[id].getPosX(),myPlayersTeamB[id].getPosY()+1 )
			&& (myPlayersTeamB[id].getPosY()+1)<=18
		)
		{
			myPlayersTeamB[id].setPosY( myPlayersTeamB[id].getPosY()+1);
		}
		//LEFT
		if(
			action==4 
			&& id<totalTeamPlayers 
			&& !checkPositionTeamB(myPlayersTeamB[id].getPosX()+1 ,myPlayersTeamB[id].getPosY())
			&& (myPlayersTeamB[id].getPosX()+1)<=9
		)
		{
			myPlayersTeamB[id].setPosX( myPlayersTeamB[id].getPosX()+1);
		}
		//RIGHT
		if(
			action==3
			&& id<totalTeamPlayers 
			&& !checkPositionTeamB(myPlayersTeamB[id].getPosX()-1 ,myPlayersTeamB[id].getPosY() )
			&& (myPlayersTeamB[id].getPosX()-1)>=6
		)
		{
			myPlayersTeamB[id].setPosX( myPlayersTeamB[id].getPosX()-1);
		}
	}
}

//------------------------------
bool playersManager::getScreenPos(int posX,int posY,char team)
{
	bool out = false;
	if(team=='a')out = checkPositionTeamA(posX, posY);
	if(team=='b')out = checkPositionTeamB(6+posX, posY);
	return out;
}

//------------------------------
int playersManager::getIdScreenPos(int posX,int posY,char team)
{
	for(int i=0;i<totalTeamPlayers;i++){
		if( team=='a' && myPlayersTeamA[i].getPosX()==posX && myPlayersTeamA[i].getPosY()==posY )return i;
		if( team=='b' && myPlayersTeamB[i].getPosX()==posX && myPlayersTeamB[i].getPosY()==posY )return i;
	}
	return -1;
}

//------------------------------
bool  playersManager::checkPositionTeamA(int posX,int posY)
{
	for(int i=0;i<totalTeamPlayers;i++)
	{
		if( myPlayersTeamA[i].getPosX()==posX && myPlayersTeamA[i].getPosY()==posY )
		{
			return true;
		}
	}
	return false;
}
//------------------------------
bool  playersManager::checkPositionTeamB(int posX,int posY)
{
	for(int i=0;i<totalTeamPlayers;i++)
	{
		if( myPlayersTeamB[i].getPosX()==posX && myPlayersTeamB[i].getPosY()==posY )
		{
			return true;
		}
	}
	return false;
}
//------------------------------
bool playersManager::getLife(int id, char team)
{
	bool out = true;
	if(team == 'a'){out = myPlayersTeamA[id].getLife();}
	if(team == 'b'){out = myPlayersTeamB[id].getLife();}
	return out;
}
//------------------------------
